<?php
namespace app\admin\model;

use think\Config;
use think\Model;
use think\Db;
use think\Session;

class PartnerModel extends Model
{
	/**
	*	合伙人申请列表
	*/
	public function getPartner($data)
	{
		$where = [];

		/* 会员姓名 */
		$u_name = empty($data['u_name']) ? '' : $data['u_name'];
		$u_name = trim($u_name);
		if(!empty($u_name)){
			$where['u_name'] = $u_name;
		}
		
		/* 申请人姓名 */
		$p_name = empty($data['p_name']) ? '' : $data['p_name'];
		$p_name = trim($p_name);
		if(!empty($p_name)){
			$where['p_name'] = $p_name;
		}

		/* 申请人身份证号 */
		$p_card = empty($data['p_card']) ? '' : $data['p_card'];
		$p_card = trim($p_card);
		if(!empty($p_card)){
			$where['p_card'] = $p_card;
		}

		/* 申请人手机号码 */
		$p_tel = empty($data['p_tel']) ? '' : $data['p_tel'];
		$p_tel = trim($p_tel);
		if(!empty($p_tel)){
			$where['p_tel'] = $p_tel;
		}

		/* 申请人联系地址 */
		$keyword = empty($data['keyword']) ? '' : $data['keyword'];
		$keyword = trim($keyword);
		if(!empty($keyword)){
			$where['p_address'] = ['like',"%$keyword%"];
		}

		/* 状态 */
		$p_status = empty($data['p_status']) ? '' : $data['p_status'];
		$p_status = trim($p_status);
		if(!empty($p_status)){
			$where['p_status'] = $p_status;
		}

		$join = [
			['sz_user u','p.uid = u.id','left']
		];

		$result = Db::table('sz_partnership')->alias('p')
				->join($join)
				->field('p.*,u.u_name')
				->where($where)
				->paginate(20,false,['query' => request()->param()]);

		return $result;
	}

	/**
	*	获取单个申请人信息
	*/
	public function getOne($id)
	{
		if($id){
			$result = Db::table('sz_partnership')->where('id',$id)->find();
			return $result;
		}
	}

	/**
	*	修改申请人信息
	*/
	public function editPartner($id,$data)
	{
		if(!empty($data)){

	        $result = Db::table('sz_partnership')->where('id',$id)->update($data);

	        return $result;
		}
	}

	/**
	*	删除合伙人信息
	*/
	public function deletePartner($id)
	{
		if(!empty($id)){

			$result = Db::table('sz_partnership')->where('id',$id)->delete();
			return $result;
		}else{

			return false;
		}
	}

	/**
	*	合伙人申请福利列表
	*/
	public function getWelfare()
	{
		$where = [];
		$where['m_name'] = '合伙人申请';
		$result = Db::table('sz_agreement')->where($where)->select();
		return $result;
	}

	/**
	*	添加合伙人申请福利提交
	*/
	public function addwelfare($data)
	{
		$data['m_name'] = '合伙人申请';
		$where = [];
		$where['m_name'] = '合伙人申请';
		$res = Db::table('sz_agreement')->where($where)->find();
		if(empty($res)){
			$result = Db::table('sz_agreement')->insert($data);

			return $result;
		}else{
			return false;
		}
		
	}

	/**
	*	修改合伙人申请福利
	*/
	public function getOneWelfare($id)
	{
		$result = Db::table('sz_agreement')->where('id',$id)->find();
		return $result;
	}

	/**
	*	修改合伙人申请福利提交
	*/
	public function editWelfarePost($id,$data)
	{	
		if($id){
			$result = Db::table('sz_agreement')->where('id',$id)->update($data);	
			return $result;
		}else{
			return false;
		}
	}

	/**
	*	删除合伙人申请福利
	*/
	public function deleteWelfare($id)
	{
		if(!empty($id)){

			$result = Db::table('sz_agreement')->where('id',$id)->delete();
			
			return $result;
			
		}else{

			return false;
		}
	}

}